package com.yazq.controller;

import com.yazq.pojo.Dept;
import com.yazq.pojo.Result;
import com.yazq.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@RequestMapping("/depts")
@RestController
public class DeptController {
    /*
     * 负责接受请求
     * @return
     */
    @Autowired
    private DeptService deptService;
    @GetMapping
    public Result returnInfo(){
        List<Dept> deptInfoService = deptService.getDeptInfoService();
        if(!deptInfoService.isEmpty()){
            return Result.success(deptService.getDeptInfoService());
        }
        else{
            return Result.error();
        }
    }
    @GetMapping("/{id}")
    public Result returnInfoById(@PathVariable("id") Integer deptId){
        return Result.success(deptService.getDeptInfoByIdService(deptId));
    }
    @DeleteMapping
    public Result returnDeleteInfo(Integer id){
        Integer rows = deptService.deleteDeptService(id);
        return Result.success(rows);
    }
    @PostMapping
    public Result addDeptInfo(@RequestBody Dept dept){
        Integer rows = deptService.addDeptService(dept);
        return Result.success(rows);
    }
    @PutMapping
    public Result updateDept(@RequestBody Dept dept){
        return Result.success(deptService.updateDeptInfo(dept));
    }
}
